package com.cg.springSecurity.security;

import com.cg.springSecurity.utils.JwtUtil;
import com.cg.springSecurity.utils.RedisUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class JwtLogoutHandler implements LogoutHandler {
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RedisUtils redisUtils;
    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        Claims claims = jwtUtil.parse(request.getHeader("Authorization"));
        if (claims != null) {
            redisUtils.removeKey(claims.getSubject());
        }
    }
}
